/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author NGOCLINH-PC
 */
public class RoleType {

    private int RoleID;
    private String RoleName;
    private int Status;

    public RoleType() {
    }

    public RoleType(int RoleID, String RoleName, int Status) {
        this.RoleID = RoleID;
        this.RoleName = RoleName;
        this.Status = Status;
    }

    public int getRoleID() {
        return RoleID;
    }

    public void setRoleID(int RoleID) {
        this.RoleID = RoleID;
    }

    public String getRoleName() {
        return RoleName;
    }

    public void setRoleName(String RoleName) {
        this.RoleName = RoleName;
    }

    public int getStatus() {
        return Status;
    }

    public void setStatus(int Status) {
        this.Status = Status;
    }

    public ArrayList<RoleType> Rselect() {
        try {
            ArrayList<RoleType> listRole = new ArrayList<RoleType>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM RoleType");
            while (rs.next()) {
                RoleType role = new RoleType(rs.getInt(1), rs.getString(2), rs.getInt(3));
                listRole.add(role);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<RoleType> RselectById(int id)  {
        try {
            ArrayList<RoleType> listRole = new ArrayList<RoleType>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM RoleType where RoleID='" + id + "'");
            while (rs.next()) {
                RoleType role = new RoleType(rs.getInt(1), rs.getString(2), rs.getInt(3));
                listRole.add(role);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void Rinsert(String PayName, int PayStatus) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("insert into RoleType values('" + PayName + "','" + PayStatus + "')");
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Insert Successful!");
                //response.sendRedirect("");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Rupdate(int CateID, String CateName, int CateStatus) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update RoleType SET RoleName='" + CateName + "', Status='" + CateStatus + "' WHERE RoleID='" + CateID + "'");
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Rdelete(int CateID) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM RoleType WHERE RoleID='" + CateID + "'");
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
